package cardmodel

import (
	"context"
	"group-card/common/tool"
	"group-card/model/entity"
	"xorm.io/xorm"
)

var _ CardSkinModel = (*customCardSkinModel)(nil)

type (
	// CardSkinModel is an interface to be customized, add more methods here,
	// and implement the added methods in customCardSkinModel.
	CardSkinModel interface {
		cardSkinModel
		List(ctx context.Context, req *entity.CardSkinReq) ([]*entity.CardSkinResp, int64, error) // 分页查询
	}

	customCardSkinModel struct {
		*defaultCardSkinModel
	}
)

// NewCardSkinModel returns a model for the database table.
func NewCardSkinModel(conn *xorm.Engine) CardSkinModel {
	return &customCardSkinModel{
		defaultCardSkinModel: newCardSkinModel(conn),
	}
}

func (m *defaultCardSkinModel) List(ctx context.Context, req *entity.CardSkinReq) ([]*entity.CardSkinResp, int64, error) {
	sql := m.conn.Table(m.table)

	if req.Columns != "" {
		sql.Select(req.Columns)
	}

	if req.Name != "" {
		sql.Where("name like ?", "%"+req.Name+"%")
	}

	if req.FaceValue != 0 {
		sql.Where("face_value = ?", req.Status)
	}

	if req.Type != 0 {
		sql.Where("type = ?", req.Type)
	}

	list := make([]*entity.CardSkinResp, 0)
	count, err := sql.Limit(int(req.PageSize), int((req.PageNum-1)*req.PageSize)).FindAndCount(&list)
	if err != nil {
		tool.GLogLine("获取卡面列表失败 defaultCardSkinModel List:", err)
		return nil, 0, err
	}

	return list, count, err
}
